/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.regex.*;

/**
 *
 * @author Dung Doan
 */
public class HavingType {
    private String m_func;
    private String m_operator;
    private String m_table;
    private String m_attribute;
    private Object m_value;
    
    public HavingType()
    {
        m_func = m_operator = m_table = m_attribute = null;
        m_value = null;
    }
    public HavingType(String command)
    {
        String REGEX = new String();
        //System.out.println(command);
        if (command.indexOf("=") != -1)
            REGEX = m_operator = "=";
        if (command.indexOf(">") != -1)
            REGEX = m_operator = ">";
        if (command.indexOf("<") != -1)
            REGEX = m_operator = "<";
        if (command.indexOf(">=") != -1)
            REGEX = m_operator = ">=";
        if (command.indexOf("<=") != -1)
            REGEX = m_operator = "<=";
        if (command.indexOf("!=") != -1)
            REGEX = m_operator = "!=";
        
        Pattern p = Pattern.compile(REGEX);
        String[] items = p.split(command);
        for (int i = 0 ; i < items.length ; i++)
            items[i] = items[i].trim();
        
        if (items.length != 2)
        {
            m_attribute = null;
            m_value = null;
            return;
        }
        
        m_attribute = items[0];
        m_value = items[1];
        
        
        String str = m_attribute.toUpperCase();
        int index = createFunc(str);
        if (m_func == null)
            return;
        m_attribute = EliminateOpenCloseOp(m_attribute.substring(index));
        REGEX = "\\.";
        p = Pattern.compile(REGEX);
        items = p.split(m_attribute);
        if (items.length == 2)
        {
            m_table = items[0];
            m_attribute = items[1];   
        }
        else
            m_table = null;
        
            
    }
    
    public int createFunc(String str)
    {
        int index = str.indexOf("SUM");
        if (index >= 0)
        {
            setM_func("sum");
            return index + 3;
        }
        index = str.indexOf("AVG");
        if (index >= 0)
        {
            setM_func("avg");
            return index + 3;
        }
        setM_func(null);
        return -1;
        
    }
    
    public String EliminateOpenCloseOp(String str)
    {
        
        
        while (str.charAt(0) == '(')
        	str = str.substring(1);
        while (str.charAt(str.length() - 1) == ')')
        	str = str.substring(0, str.length() - 1);
        return str;
        //return str;
            
    }
    
    public Boolean isValid()
    {
        if (getM_func() == null || getM_attribute() == null || getM_value() == null || getM_operator() == null)
            return false;
        return true;
    }

    /**
     * @return the m_func
     */
    public String getM_func() {
        return m_func;
    }

    /**
     * @param m_func the m_func to set
     */
    public void setM_func(String m_func) {
        this.m_func = m_func;
    }

    /**
     * @return the m_operator
     */
    public String getM_operator() {
        return m_operator;
    }

    /**
     * @param m_operator the m_operator to set
     */
    public void setM_operator(String m_operator) {
        this.m_operator = m_operator;
    }

    /**
     * @return the m_table
     */
    public String getM_table() {
        return m_table;
    }

    /**
     * @param m_table the m_table to set
     */
    public void setM_table(String m_table) {
        this.m_table = m_table;
    }

    /**
     * @return the m_attribute
     */
    public String getM_attribute() {
        return m_attribute;
    }

    /**
     * @param m_attribute the m_attribute to set
     */
    public void setM_attribute(String m_attribute) {
        this.m_attribute = m_attribute;
    }

    /**
     * @return the m_value
     */
    public Object getM_value() {
        return m_value;
    }

    /**
     * @param m_value the m_value to set
     */
    public void setM_value(Object m_value) {
        this.m_value = m_value;
    }
}
